#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
计算腾讯云HAI最大部署数量
基于腾讯云API频率限制文档分析
"""

def calculate_max_deployment():
    """计算最大部署数量"""
    print("📊 腾讯云HAI最大部署数量计算")
    print("=" * 60)
    print("参考文档: https://cloud.tencent.com/document/product/1278/109059")
    print()
    
    # 腾讯云API频率限制参数
    api_limits = {
        "default_rate_per_second": 20,  # 默认每秒20次请求
        "time_window_seconds": 1,       # 时间窗口1秒
        "max_concurrent_requests": 100, # 最大并发请求数
        "request_timeout": 60,          # 请求超时时间
        "retry_attempts": 3,            # 重试次数
        "retry_delay": 1                # 重试延迟
    }
    
    print("🔍 API频率限制参数:")
    for key, value in api_limits.items():
        print(f"  {key}: {value}")
    print()
    
    # 计算不同时间窗口的最大部署数量
    time_windows = [1, 10, 60, 300, 600]  # 1秒, 10秒, 1分钟, 5分钟, 10分钟
    
    print("📈 不同时间窗口的最大部署数量:")
    print("-" * 50)
    print(f"{'时间窗口':<10} {'最大请求数':<12} {'成功率':<10} {'备注'}")
    print("-" * 50)
    
    for window in time_windows:
        max_requests = api_limits["default_rate_per_second"] * window
        
        # 考虑重试和错误率
        success_rate = 0.95  # 95%成功率
        effective_requests = int(max_requests * success_rate)
        
        # 考虑并发限制
        if effective_requests > api_limits["max_concurrent_requests"]:
            effective_requests = api_limits["max_concurrent_requests"]
        
        print(f"{window}秒{'':<6} {effective_requests:<12} {success_rate*100:.0f}%{'':<6} {'理论最大值'}")
    
    print()
    
    # 实际部署策略分析
    print("🚀 实际部署策略分析:")
    print("-" * 50)
    
    strategies = [
        {
            "name": "保守策略",
            "rate_per_second": 10,
            "description": "使用50%的API限制，确保稳定性"
        },
        {
            "name": "平衡策略", 
            "rate_per_second": 15,
            "description": "使用75%的API限制，平衡速度和稳定性"
        },
        {
            "name": "激进策略",
            "rate_per_second": 18,
            "description": "使用90%的API限制，最大化部署速度"
        }
    ]
    
    for strategy in strategies:
        print(f"\n📋 {strategy['name']}:")
        print(f"  每秒请求数: {strategy['rate_per_second']}")
        print(f"  描述: {strategy['description']}")
        
        # 计算1分钟内的最大部署数量
        max_in_1min = strategy["rate_per_second"] * 60
        print(f"  1分钟内最大部署: {max_in_1min} 个实例")
        
        # 计算5分钟内的最大部署数量
        max_in_5min = strategy["rate_per_second"] * 300
        print(f"  5分钟内最大部署: {max_in_5min} 个实例")
    
    print()
    
    # 批处理策略分析
    print("🔄 批处理策略分析:")
    print("-" * 50)
    
    batch_strategies = [
        {"batch_size": 20, "interval": 1, "description": "小批次快速部署"},
        {"batch_size": 50, "interval": 3, "description": "中批次平衡部署"},
        {"batch_size": 100, "interval": 5, "description": "大批次稳定部署"}
    ]
    
    for batch in batch_strategies:
        print(f"\n📦 批次大小: {batch['batch_size']}, 间隔: {batch['interval']}秒")
        print(f"  描述: {batch['description']}")
        
        # 计算10分钟内的部署数量
        batches_in_10min = (10 * 60) // (batch['interval'] + 1)  # +1是处理时间
        total_deployed = batches_in_10min * batch['batch_size']
        print(f"  10分钟内可部署: {total_deployed} 个实例")
    
    print()
    
    # 结论和建议
    print("🎯 结论和建议:")
    print("=" * 60)
    print("基于腾讯云API频率限制分析:")
    print()
    print("1. 📊 理论最大值:")
    print("   - 每秒最多: 20个请求")
    print("   - 每分钟最多: 1,200个请求")
    print("   - 实际建议: 使用80%限制，即每分钟960个请求")
    print()
    print("2. 🚀 推荐部署策略:")
    print("   - 批次大小: 50个实例/批次")
    print("   - 批次间隔: 3秒")
    print("   - 并发线程: 20个")
    print("   - 预计部署时间: 约4-6分钟")
    print()
    print("3. ⚠️ 注意事项:")
    print("   - 需要监控API调用频率")
    print("   - 实现指数退避重试机制")
    print("   - 考虑腾讯云后端资源限制")
    print("   - 建议分批部署，避免触发限流")
    print()
    print("4. 🎯 实际最大部署数量:")
    print("   - 单次部署: 建议不超过100个实例")
    print("   - 连续部署: 每分钟最多960个实例")
    print("   - 大规模部署: 需要申请提高限频")

if __name__ == "__main__":
    calculate_max_deployment()
